package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Override
    public PageResult<Business> selectList(BusinessDto businessDto) {
        Page <Business> p = this.baseMapper.selectBsList(new Page<Business>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<>(p.getTotal(),p.getRecords());
    }

    @Override
    public Business selectBusById(Integer id) {
        return businessMapper.selectBusById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateList(Business business) {
        //1.bus表
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //2.track表
        BusinessTrackRecord trackRecord = new BusinessTrackRecord();
        trackRecord.setBusinessId(business.getId());
        trackRecord.setUserId(business.getUserId());
        trackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        trackRecord.setTrackStatus(2);
        trackRecord.setKeyItems(business.getKeyItems().toString());
        trackRecord.setNextTime(business.getNextTime());
        trackRecord.setRecord(business.getRecord());
        trackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(trackRecord);
    }
}
